<?php

namespace app\roomcall\controller;


use app\common\model\ViperCard;
use app\common\model\ViperUser;
use app\common\model\VipRechargeLog;

class Card extends Home
{
    public $noNeedLogin = ['check_is_pay'];

    /**
     *  会员卡查询
     **/
    public function vip_serch()
    {
        if ($this->request->isPost()) {
            $phone = $this->request->post('phone');
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->serch_phone($phone, $this->store_id);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     *  会员卡详情
     **/
    public function card_info()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->card_info($id, $this->store_id);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     * 充值视图
     */
    public function get_rechare_tem()
    {
        if ($this->request->isPost()) {
            $vip_type_id = $this->request->post('viper_type_id');
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->get_recharge_tem($this->store_id, $vip_type_id);
            $info = array(
                'list' => $list,
            );
            $this->result('获取成功', $info, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     * 生成支付二维码
     */
    public function recharge_do()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $user_id = $this->userInfo['id'];
            $recharge_tempid = $this->request->post('recharge_tempid');
            $pay_type = $this->request->post('pay_type');
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->recharge_do($this->store_id, $id, $pay_type, $recharge_tempid, $this->room_id, $user_id);
            if ($list['status']) {
                $this->result($list['msg'], $list['data'], 200);
            } else {
                $this->result($list['msg'], '', 204);
            }
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     * 查询订单是否已经支付
     */
    public function check_is_pay()
    {
        $id = $this->request->post('id');
        $re = VipRechargeLog::where('id', $id)->find();
        if ($re['pay_status'] == '1') {
            $this->result('', '', 200);
        } else {
            $this->result('', '', 204);
        }
    }

    /**
     *获取开卡模板
     */
    public function get_open_tem()
    {
        if ($this->request->isPost()) {
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->get_cardtype($this->store_id);
            $info = array(
                'list' => $list,
            );
            $this->result('获取成功', $info, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     *  会员开卡
     **/
    public function open_card()
    {
        if ($this->request->isPost()) {
            $phone = $this->request->post('phone');
            $user = ViperUser::where('phone', $phone)
                ->where('store_id', $this->store_id)
                ->find();
            if (empty($user)) {
                $this->result('用户不存在', '', 204);
            }
            $vipcard = ViperCard::where('store_id', $this->store_id)
                ->where('user_id', $user['id'])
                ->find();
            if (!empty($vipcard)) {
                $this->result('你已经开通过vip了', '', 204);
            }
            $vip_type_id = $this->request->post('viper_type_id');
            $template_id = $this->request->post('template_id');
            $passwd = $this->request->post('passwd');
            $birthday = $this->request->post('birthday');
            $pay_type = $this->request->post('pay_type');
            $VipcardModel = new ViperCard();
            $list = $VipcardModel->open_Card($this->store_id, $vip_type_id, $template_id, $user['id'], $this->userInfo['id'], $passwd, $birthday, $pay_type, $this->room_id);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }

    }
}
